
Namespace DotNetNuke.Modules.ActiveForums.Queue
    Public Class Controller
        Public Shared Sub Add(ByVal EmailFrom As String, ByVal EmailTo As String, ByVal EmailSubject As String, ByVal EmailBody As String, ByVal EmailBodyPlainText As String, ByVal EmailCC As String, ByVal EmailBCC As String)
            Try
                DataProvider.Instance.Queue_Add(EmailFrom, EmailTo, EmailSubject, EmailBody, EmailBodyPlainText, EmailCC, EmailBCC)
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try

        End Sub
    End Class
    Public Class Scheduler
        Inherits DotNetNuke.Services.Scheduling.SchedulerClient

        Dim SMTPServer As String
        Dim SmtpUserName As String
        Dim SMTPPassword As String
        Dim SMTPAuthentication As String

        Public Sub New(ByVal objScheduleHistoryItem As DotNetNuke.Services.Scheduling.ScheduleHistoryItem)
            MyBase.New()
            Me.ScheduleHistoryItem = objScheduleHistoryItem
        End Sub
        Public Overrides Sub DoWork()
            Try
                Dim intQueueCount As Integer = 0
                intQueueCount = ProcessQueue()
                ScheduleHistoryItem.Succeeded = True
                ScheduleHistoryItem.AddLogNote("Processed " & intQueueCount.ToString & " messages")
            Catch ex As Exception
                ScheduleHistoryItem.Succeeded = False
                ScheduleHistoryItem.AddLogNote("Process Queue Failed. " & ex.ToString)
                Errored(ex)
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
        End Sub
        Private Function ProcessQueue() As Integer
            Dim intQueueCount As Integer = 0
            Try
                'Get Host SMTP Settings
                Dim objHost As Hashtable = DotNetNuke.Entities.Portals.PortalSettings.GetHostSettings()
                'Get Queue

                Dim dr As IDataReader = DataProvider.Instance.Queue_List()
                While dr.Read
                    intQueueCount += 1
                    Dim objEmail As New Message
                    objEmail.Subject = dr("EmailSubject").ToString
                    objEmail.SendFrom = dr("EmailFrom").ToString
                    objEmail.SendTo = dr("EmailTo").ToString
                    objEmail.Body = dr("EmailBody").ToString
                    objEmail.BodyText = dr("EmailBodyPlainText").ToString
                    objEmail.SmtpServer = objHost("SMTPServer").ToString
                    objEmail.SmtpUserName = objHost("SMTPUsername").ToString
                    objEmail.SmtpPassword = objHost("SMTPPassword").ToString
                    objEmail.SmtpAuthentication = objHost("SMTPAuthentication").ToString
                    objEmail.SmtpSSL = objHost("SMTPEnableSSL").ToString

                    Dim canDelete As Boolean = objEmail.SendMail()
                    If canDelete Then
                        Try
                            DataProvider.Instance.Queue_Delete(CType(dr("Id"), Integer))
                        Catch ex As Exception
                            DotNetNuke.Services.Exceptions.LogException(ex)
                        End Try
                    Else
                        intQueueCount = intQueueCount - 1
                    End If
                End While
                dr.Close()
                dr.Dispose()

                Return intQueueCount
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
                Return -1
            End Try

        End Function

    End Class
    Public Class Message
        Public Subject As String
        Public SendFrom As String
        Public SendTo As String
        Public Body As String
        Public BodyText As String
        'Public Recipients As ArrayList
        Public SmtpServer As String
        Public SmtpUserName As String
        Public SmtpPassword As String
        Public SmtpAuthentication As String
        Public SmtpSSL As String
        'Public Subs As List(Of SubscriptionInfo)


        Public Function SendMail() As Boolean
            Try
                Dim objHost As Hashtable = Nothing
                If SmtpServer = "" Then
                    objHost = DotNetNuke.Entities.Portals.PortalSettings.GetHostSettings()
                    SmtpServer = Convert.ToString(objHost("SMTPServer"))
                    SmtpUserName = Convert.ToString(objHost("SMTPUsername"))
                    SmtpPassword = Convert.ToString(objHost("SMTPPassword"))
                    SmtpAuthentication = Convert.ToString(objHost("SMTPAuthentication"))
                    SmtpSSL = Convert.ToString(objHost("SMTPEnableSSL"))
                End If
                'Dim Email As New System.Net.Mail.MailMessage

                'Email.From = New System.Net.Mail.MailAddress(SendFrom)
                'Email.To.Add(New System.Net.Mail.MailAddress(SendTo))
                'Email.Subject = Subject
                'Email.IsBodyHtml = True
                'Email.Body = Body
                Dim subs As New List(Of SubscriptionInfo)
                Dim si As New SubscriptionInfo
                si.Email = SendTo
                si.DisplayName = String.Empty
                si.LastName = String.Empty
                si.FirstName = String.Empty
                subs.Add(si)
                Dim oEmail As New Email
                oEmail.UseQueue = False
                oEmail.Recipients = subs
                oEmail.Subject = Subject
                oEmail.From = SendFrom
                oEmail.BodyText = BodyText
                oEmail.BodyHTML = Body
                oEmail.SmtpServer = SmtpServer
                oEmail.SmtpUserName = SmtpUserName
                oEmail.SmtpPassword = SmtpPassword
                oEmail.SmtpAuthentication = SmtpAuthentication
                oEmail.SmtpSSL = SmtpSSL
                Try
                    Dim objThread As New System.Threading.Thread(AddressOf oEmail.Send)
                    objThread.Start()
                    Return True
                Catch ex As Exception
                    DotNetNuke.Services.Exceptions.LogException(ex)
                    Return False
                End Try
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
                Return False
            End Try
        End Function
    End Class

End Namespace
